﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <h1 class="bold-large">角色关联权限</h1>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">角色选择</label>
            <div class="layui-input-block">
                <select name="interest" lay-filter="aihao" id="rolesId">
                    <option value="0" selected>请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">权限选择</label>
                <div class="layui-input-block">
                    <div id="permissionId"></div> <!-- 新的容器用于放置动态生成的复选框 -->
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1" onclick="AddRolePermission(event)">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</body>
</html>

<script>
    var form = layui.form;
    var $ = layui.jquery;

    $(function () {
        GetPermission()  // 权限
        GetRoles() //角色
    })
    //角色权限添加
    function AddRolePermission(event) {
        var permissionsArr = [];
        $("[name='permissions']:checked").each(function () {
            permissionsArr.push($(this).val());
        });
        let data = {
            rolesId: $("#rolesId").val(),
            permissions: permissionsArr
        }
        console.log(data)
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/UserRolePermission/CreateRolePermission",
            type: "post",
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json",
            success: res => {
                console.log(res)
                if (res.code == 0) {
                    alert(res.msg)
                } else {
                    alert(res.msg)
                }
            }
        })
        event.preventDefault(); // 阻止表单默认提交
    }
    // 角色列表
    function GetRoles() {
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/Roles/QueryRoles",
            data: JSON.stringify({}),
            type: "post",
            dataType: "json",
            contentType: "application/json",
            success: res => {
                $.each(res.data, function () {
                    $("#rolesId").append(`<option value="${this.rolesId}">${this.rolesName}</option>`);
                });
                form.render('select'); // 重新渲染下拉框
            },
        });
    }

    //权限列表
    function GetPermission() {
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/Permissions/SearchPermission",
            data: JSON.stringify({}),
            type: "post",
            dataType: "json",
            contentType: "application/json",
            success: res => {
                $.each(res.data, function () {
                    // $("#permissionId").empty();
                    $("#permissionId").empty(); // 清空之前的复选框

                    $.each(res.data, function () {
                        const checkbox = $(`<input type="checkbox" name="permissions" title="${this.permissionName}" value="${this.permissionId}" template-id="permission_${this.permissionId}" lay-skin="primary">`);
                        $("#permissionId").append(checkbox);
                    });
                });
                form.render(); //重新渲染整个表单
            },
        });
    }
</script>